import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

import Qt.labs.platform 1.1
import QtQuick.Controls.Material


ApplicationWindow {
    id: root
    width: 1200
    height: 720
    visible: true
    title: qsTr("Prompt Creator")

    Material.theme: Material.Dark
    Material.accent: Material.Teal
    function generateHistoryText() {
        var content = "";
        for (var i = 0; i < historyModel.count; i++) {
            var item = historyModel.get(i);
            content += "Timestamp: " + item.timestamp + "\n";
            content += "Prompt: " + item.prompt + "\n\n";
        }
        return content;
    }

    Component.onCompleted: {
        var savedHistory = JSON.parse(fileHelper.loadHistory());
        if (savedHistory) {
            for (var i = 0; i < savedHistory.length; i++) {
                historyModel.append({
                    prompt: savedHistory[i].prompt,
                    timestamp: savedHistory[i].timestamp,
                    basePrompt: savedHistory[i].basePrompt,
                    dynamicFields: savedHistory[i].dynamicFields
                });
            }
        }
    }

    ListModel {
        id: historyModel
    }



    RowLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 20

        ColumnLayout {
            Layout.fillWidth: true
            Layout.fillHeight: true

            EditorView {
                id: editorView
                Layout.fillWidth: true
                Layout.fillHeight: true
                onComposeClicked: (finalPrompt, historyItem) => {
                    historyModel.insert(0, historyItem);

                    if (historyModel.count > 20) {
                        historyModel.remove(20);
                    }

                    var historyArray = [];
                    for (var i = 0; i < historyModel.count; i++) {
                        var item = historyModel.get(i);
                        historyArray.push(item);
                    }
                    fileHelper.saveHistory(JSON.stringify(historyArray));
                }
            }
        }

        HistoryView {
            id: historyView
            historyModel: historyModel

            onItemClicked: (historyItem) => {
                editorView.basePromptInput.text = historyItem.basePrompt || "";
                editorView.outputArea.text = historyItem.prompt || "";

                editorView.dynamicFieldsModel.clear();
                if (historyItem.dynamicFields) {
                    for (var i = 0; i < historyItem.dynamicFields.length; i++) {
                        editorView.dynamicFieldsModel.append({
                            "label": historyItem.dynamicFields[i].label,
                            "value": historyItem.dynamicFields[i].value,
                            "placeholder": historyItem.dynamicFields[i].placeholder
                        });
                    }
                }
            }

            onClearHistoryClicked: () => {
                historyModel.clear()
                fileHelper.saveHistory("[]")
            }

            onExportHistoryClicked: () => {
                saveDialog.open()
            }
        }
    }

    FileDialog {
        id: saveDialog
        title: "Save As"
        fileMode: FileDialog.SaveFile
        onAccepted: {
            fileHelper.write(saveDialog.file, generateHistoryText());
        }
        onRejected: {
            var filename = new Date().toISOString().replace(/:/g, '-').replace(/\..+/, '') + ".txt";
            fileHelper.write(filename, generateHistoryText());
        }
    }
}
